package com.hippo.javacore.javaiostream.iostream;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * @ClassName ObjectOutputStreamOpt
 * @Description TODO 对象字节输出流操作类
 * @Author tangxl
 * @create 2023-04-07 10:38
 **/
public class ObjectOutputStreamOpt {
    public static void main(String[] args) {
        try(
                // 创建一个对象字节输出流对象
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("study-commons/java-file-iostream/src/main/resources/file/a.txt"));
                ) {
            // 创建一个学生对象
            Student s = new Student(1, "张三", 18,"123456");
            // 写入对象
            oos.writeObject(s);

        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    // 注意: 要想序列化对象, 需要实现Serializable接口
    static class Student implements Serializable {
        int id;
        String name;
        int age;
        // transient关键字修饰的属性不会被序列化
        transient String password;
    }
}
